/*
 * @lc app=leetcode.cn id=199 lang=cpp
 *
 * [199] 二叉树的右视图
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> rightSideView(TreeNode* root) {
        //层次遍历
        //BFS
        //借用队列完成
        queue<TreeNode*> q;
        vector<int> ans;

        if(root == nullptr) return ans;

        q.push(root);

        while(!q.empty()){
            int len = q.size();
            TreeNode* p;
            for(int i = 0; i < len; i++){
                p = q.front();
                q.pop();
                if(p->left != nullptr) q.push(p->left);
                if(p->right != nullptr) q.push(p->right);
            }
            ans.push_back(p->val);
        }
        return ans;
    }
};
// @lc code=end

